# GF_PY3_CLASS_Finance_Indicator_MACD_s_Breakpoint_Recovery.py
# Create By GF 2024-01-22 18:16

import json
# ..................................................
import GF_PY3_CLASS_Python_3_List_and_JSON_File
import GF_PY3_CLASS_Finance_Indicator_MACD

# ##################################################

class Finance_Indicator_MACD_s_Breakpoint_Recovery(GF_PY3_CLASS_Python_3_List_and_JSON_File.Python_3_List_and_JSON_File,
                                                   GF_PY3_CLASS_Finance_Indicator_MACD.Finance_Indicator_MACD):

    def __init__(self):

        self.Breakpoint_Exists:int = 0
        self.Historical_Digit_of_Previous_MACD_DEA:float = None
        # ..........................................
        self.JSON_Digit_of_Previous_MACD_DEA:float = None

    def MACD_DEA_s_Breakpoint_Recovery(self, Table:str, Time:str, Code:str, ROW_NUM:int, MAX_ROW_NUM:int, MACD_DIF:float) -> float:

        MACD_DEA_Value:float = None

        if (ROW_NUM == 2):

            JSON_List = self.JSON_File_to_Python_3_List("./GF_PY3_CLASS_Finance_Indicator_Calculation_Breakpoint.json")
            JSON_List = [Line for Line in JSON_List if (Line.get("Table", '') == Table)]
            JSON_List = [Line for Line in JSON_List if (Line.get("Code",  '') == Code )]
            JSON_List = [Line for Line in JSON_List if ("MACD_DEA" in Line.keys()     )]
            # ......................................
            JSON_Dict = JSON_List[-1] if (JSON_List != []) else {}
            # ......................................
            self.Historical_Digit_of_Previous_MACD_DEA = JSON_Dict.get(f"MACD_DEA", None)
            # ......................................
            if (self.Historical_Digit_of_Previous_MACD_DEA == None): self.Breakpoint_Exists = 0
            if (self.Historical_Digit_of_Previous_MACD_DEA != None): self.Breakpoint_Exists = 1

        if (self.Breakpoint_Exists == 0):

            MACD_DEA_Value = self.MACD_DEA(ROW_NUM = (ROW_NUM - 1), MACD_DIF = MACD_DIF)

        if (self.Breakpoint_Exists == 1 and ROW_NUM == 2):  # 仅在 "ROW_NUM == 2" 时拷贝历史计算数据

            self.JSON_Digit_of_Previous_MACD_DEA = self.Historical_Digit_of_Previous_MACD_DEA

        if (self.Breakpoint_Exists == 1):

            MACD_DEA_Value = self.MACD_DEA(ROW_NUM = ROW_NUM, MACD_DIF = MACD_DIF)

        if (ROW_NUM == MAX_ROW_NUM):

            JSON_List = self.JSON_File_to_Python_3_List("./GF_PY3_CLASS_Finance_Indicator_Calculation_Breakpoint.json")
            JSON_List.append({"Table": Table, "Time":Time, "Code": Code, "MACD_DEA": MACD_DEA_Value})
            # ......................................
            self.Python_3_List_to_JSON_File(Python_3_List = JSON_List, JSON_File = "./GF_PY3_CLASS_Finance_Indicator_Calculation_Breakpoint.json")

        return MACD_DEA_Value

# EOF Signed by GF.
